package cn.itcast.dao;

import cn.itcast.Permission;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Select;

import java.util.List;

public interface PermissionDao {

    /**
     * 根据roleId查询权限资源
     * @param roleId
     * @return
     */
    @Select("select * from permission where id in " +
            "(select permissionId from role_permission where roleId = #{roleId})")
    public List<Permission> findByRoleId(String roleId);

    /**
     * 查询所有权限资源方法
     * @return
     */
    @Select("select * from permission")
    public List<Permission> findAll();


    /**
     * 根据id查询资源权限
     * @param id
     * @return
     * @throws Exception
     */
    @Select("select * from permission where id = #{id}")
    Permission findById(String id) throws Exception;

    /**
     * 添加权限
     * @param permission
     * @throws Exception
     */
    @Insert("insert into permission (permissionName,url) values (#{permissionName},#{url})")
    void save(Permission permission) throws Exception;

    /**
     * 根据roleId查找该角色还可以添加的权限
     * @param roleId
     * @return
     */
    @Select("select * from permission where id not in " +
            "(select roleId from role_permission where roleId = #{roleId})")
    List<Permission> findOtherPermission(String roleId);
}
